//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using CommunityServer.Controls;
using CommunityServer.Components;
using System.Collections;


namespace CommunityServer.Controls
{
	public class ContactPopupMenu : ClientPopulatedPopupMenu
	{
		CSContext csContext;

		#region Event Handlers

		protected override void OnInit(System.EventArgs e)
		{
			csContext = CSContext.Current;
			this.ResourceName = "ContactPopupMenu_Text";

			if (csContext.User.IsAnonymous)
				this.Visible = false;
		}

		protected override void OnPreRender(System.EventArgs e)
		{

            if(this.User == null && this.UserID > -1)
            {
                this.User = Users.GetUser(this.UserID, false);
            }
			
            if (this.User == null)
                this.Visible = false;
            else
            {
                this.Parameters = new string[8];
                this.Parameters[0] = this.User.UserID.ToString();
                this.Parameters[1] = Globals.IsNullorEmpty(this.User.DisplayName) ? this.User.Username : this.User.DisplayName;
                this.Parameters[2] = Globals.IsNullorEmpty(this.User.Email) ? "1" : "0";
                this.Parameters[3] = this.User.IsAnonymous ? "0" : "1";
                this.Parameters[4] = this.User.Profile.WebAddress;
                this.Parameters[5] = this.User.Profile.WebLog;
                this.Parameters[6] = this.User.Profile.WebGallery;
				this.Parameters[7] = SiteUrls.Instance().SearchByUser(this.User.UserID);
				
			

                if (this.ShowBackgroundImageIcon)
                    this.Style["background-image"] = "url(" + Globals.GetSkinPath() + "/images/common/contact.gif)";
            }
			base.OnPreRender(e);
		}

		#endregion

		#region Render Methods

		protected override ArrayList ClientMenuItemFormats
		{
			get
			{
				ArrayList formats = new ArrayList();
				ClientMenuItemFormat format;

				// 0 - UserID, 1 - UserName or DisplayName, 2 - HasEmail, 3 - IsAnonymouse, 4 - HomePageURL, 5 - WeblogURL, 6 - GalleryURL, 7 - search URL

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return parms[2];')";
				format.Icon = Globals.GetSkinPath() + "/images/common/email.gif";
				format.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("ContactPopupMenu_Email"), "{1}");
				format.ClientSideCommand = ModalHelper.BuildHandler(this.Page, SiteUrls.Instance().EmailToUser(99999), 600, 400, "null", false).Replace("99999", "{0}");
				formats.Add(format);

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return parms[3];')";
				format.Icon = Globals.GetSkinPath() + "/images/common/privatemessage.gif";
				format.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("ContactPopupMenu_PrivateMessage"), "{1}");
				format.NavigateUrl = Globals.GetSiteUrls().PrivateMessage(99999).Replace("99999", "{0}");
				formats.Add(format);

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return parms[4];')";
				format.Icon = Globals.GetSkinPath() + "/images/common/homepage.gif";
				format.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("ContactPopupMenu_HomePage"), "{1}");
				format.NavigateUrl = "{4}";
				format.Target = "_blank";
				formats.Add(format);

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return parms[5];')";
				format.Icon = Globals.GetSkinPath() + "/images/common/weblog.gif";
				format.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("ContactPopupMenu_Weblog"), "{1}");
				format.NavigateUrl = "{5}";
				format.Target = "_blank";
				formats.Add(format);

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return parms[6];')";
				format.Icon = Globals.GetSkinPath() + "/images/common/webgallery.gif";
				format.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("ContactPopupMenu_WebGallery"), "{1}");
				format.NavigateUrl = "{6}";
				format.Target = "_blank";
				formats.Add(format);
				
				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return true;')";
				format.Icon = Globals.GetSkinPath() + "/images/common/profile.gif";
				format.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("ContactPopupMenu_Profile"), "{1}");
				format.NavigateUrl = Globals.GetSiteUrls().UserProfile(99999).Replace("99999", "{0}");
				formats.Add(format);

				format = new ClientMenuItemFormat();
				format.ClientSideDisplayFunction = "new Function('parms', 'return true;')";
				format.Icon = Globals.GetSkinPath() + "/images/common/search.gif";
				format.Text = String.Format(CommunityServer.Components.ResourceManager.GetString("ContactPopupMenu_SearchFor"), "{1}");
				format.NavigateUrl = "{7}";
				formats.Add(format);

				return formats;
			}
		}

		#endregion

		#region Properties

		public virtual bool ShowBackgroundImageIcon
		{
			get
			{
				object state = ViewState["ShowBackgroundImageIcon"];
				if (state == null)
					return true;
				else
					return (bool) state;
			}
			set
			{
				ViewState["ShowBackgroundImageIcon"] = value;
			}
		}

		public virtual int UserID
		{
			get
			{
                if(this.User != null)
                    return this.User.UserID;

				object state = ViewState["UserID"];
				if (state == null)
					return csContext.UserID;
				else
					return (int) state;
			}
			set
			{
				ViewState["UserID"] = value;
			}
		}

        private User _user = null;
        public User User
        {
            get{ return _user;}
            set{_user = value;}
        }

		#endregion
	}
}
